﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KTXD.Data;
using KTXD.Entities;
namespace UserKTXD.Controls
{
    public partial class ControlNewTinTuc : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtTimKiem.Attributes.Add("onkeypress", "return clickButton(event,'" + cmdTimKiem.ClientID + "')");
                loadData();
            }
        }

        protected void cmdTimKiem_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/TuKhoa/" + GetTieuDe(txtTimKiem.Text) + ".aspx");
        }

          private void loadData()
        {
            int TotalRows = 0;
            // Lấy ID của Loại Chuyên Mục Sự kiện
            TList<HomeControl> TList_HomeControl = DataRepository.HomeControlProvider.GetPaged("Id=2", "Id", 0, 0, out TotalRows);
            if (TList_HomeControl.Count > 0)
            {
                LoaiChuyenMuc ent_LoaiChuyenMuc = DataRepository.LoaiChuyenMucProvider.GetById(TList_HomeControl[0].IdLoaiChuyenMuc);
                linkTinTuc.NavigateUrl = "~/Blog/" + GetTieuDe(ent_LoaiChuyenMuc.TenLoaiChuyenMuc) + "_" + ent_LoaiChuyenMuc.Id + ".aspx";
                // Lấy ra 6 chuyên mục của loại chuyên mục sự kiện
                string WhereClause = "IdLoaiChuyenMuc=" + TList_HomeControl[0].IdLoaiChuyenMuc;
                TList<ChuyenMucCapHai> TList_ChuyenMucCapHai = DataRepository.ChuyenMucCapHaiProvider.GetPaged(WhereClause, "Id DESC", 0, 5, out TotalRows);
                if (TList_ChuyenMucCapHai.Count > 0)
                {
                    
                    Repeater1.DataSource = TList_ChuyenMucCapHai;
                    Repeater1.DataBind();
                }
            }
        }

            //string WhereClause2 = "1=1"; 
            //int TotalRows = 0;
            //TList<HomeControl> TList_HomeControl = DataRepository.HomeControlProvider.GetPaged(WhereClause2, "IdLoaiChuyenMuc", 0, 0, out TotalRows);
            //if (TList_HomeControl.Count > 0)
            //{
            //    string WhereClause = "Id=" + TList_HomeControl[0].Id;
            //    TList<LoaiChuyenMuc> TList_LoaiChuyenMuc = DataRepository.LoaiChuyenMucProvider.GetPaged(WhereClause, "Id DESC", 0, 0, out TotalRows);
            //    string WhereClause1 = "IdLoaiChuyenMuc=" + TList_LoaiChuyenMuc[0].Id;
            //    TList<SubChuyenMuc> TList_SubChuyenMuc = DataRepository.SubChuyenMucProvider.GetPaged(WhereClause1, "Id DESC", 0, 0, out TotalRows);
            //    string WhereClause3 = "IdSubChuyenMuc=" + TList_SubChuyenMuc[0].Id;
            //    TList<ChuyenMucCapHai> TList_ChuyenMucCapHai = DataRepository.ChuyenMucCapHaiProvider.GetPaged(WhereClause3, "Id DESC", 0, 0, out TotalRows);
            //    if (TList_ChuyenMucCapHai.Count > 0)
            //    {
            //        if (TList_ChuyenMucCapHai.Count > 3)
            //        {
            //            TList<ChuyenMucCapHai> TList_ChuyenMucCapHai1 = new TList<ChuyenMucCapHai>();
            //            for (int i = 0; i < 3; i++)
            //            {
            //                TList_ChuyenMucCapHai1.Add(TList_ChuyenMucCapHai[i]);
            //            }
            //            Repeater1.DataSource = TList_ChuyenMucCapHai1;
            //            Repeater1.DataBind();


            //            TList<ChuyenMucCapHai> TList_ChuyenMuc2 = new TList<ChuyenMucCapHai>();
            //            //strHTLM += "<div class='item-wrapper'>";

            //            for (int i = 3; i < TList_ChuyenMucCapHai.Count; i++)
            //            {
            //                TList_ChuyenMuc2.Add(TList_ChuyenMucCapHai[i]);
            //            }

            //            Repeater1.DataSource = TList_ChuyenMuc2;
            //            Repeater1.DataBind();
            //        }
            //        else
            //        {
            //            Repeater1.DataSource = TList_ChuyenMucCapHai;
            //            Repeater1.DataBind();
            //        }
            //    }

            //}
        //}
        public string GetSubChuyenMuc(object Id)
        {
            int s = Convert.ToInt32(Id.ToString());
            SubChuyenMuc ent_SubChuyenMuc = DataRepository.SubChuyenMucProvider.GetById(s);
            return ent_SubChuyenMuc.TenSubChuyenMuc;
        }
        //cover ttiếng việt có dấu sang không dấu
        public string GetTieuDe(object Char)
        {
            string s = Char.ToString();
            return Common.ConvertToUnSign(s);
        }
        
    }

}
